package com.aco.party.util;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chentong 用于页面字符串转义处理的工具类
 */
public class HTMLUtil {
    private final static String DEFAULT_ENCODE = "utf8";

    /**
     * 按字节截断文本。
     *
     * @param str
     *            需要处理的字符串
     * @param length
     *            最大字节长度
     * @param addShenglu
     *            true：截断后添加自定义尾部文本，默认为"..." ,false:如果是刚好等于最大长度则截取到最大长度;否则否则截取到前一个字符
     * @return 截断后的文本。最后一个字符
     */
    public static String truncateStrByByte(String str, int length, boolean addShenglu) {
        if (addShenglu) {
            length--; // 省略号占两个字节,需要先减一减免显示时不全
        }
        try {
            // a.nothing
            if (str == null) {
                return "";
            }
            // b.within the change(more possible)
            else if (str.getBytes(DEFAULT_ENCODE).length <= length) {
                return str;
            }
            // c.over the limit
            else {
                int count = 0;
                int i = 0;
                for (; i < str.length(); i++) {
                    String tmp = str.substring(i, i + 1);
                    count += tmp.getBytes(DEFAULT_ENCODE).length;
                    if (count >= length) { // 循环必将在此退出
                        break;
                    }
                }
                if (addShenglu)
                    return str.substring(0, i) + "...";
                else if (count == length)
                    return str.substring(0, (i + 1)); // NOTE:不加省略号并且是刚好等于限制时截取到最长(**新鲜土豆中播客昵称需求**)
                else
                    return str.substring(0, i); // 截取限制长的前一个字符
            }
        } catch (Exception e) {
            return "";
        }
    }

    public final static String js(String content) {
        return escapeJs(content);
    }

    private final static String escapeJs(String content) {
        if (StringUtils.isBlank(content)) {
            return "";
        }
        content = content.replace("\\", "\\\\");
        content = content.replace("'", "\\'");
        content = content.replace("\"", "\\\"");
        content = content.replace("/", "\\/");
        content = content.replace("\b", "\\b");
        content = content.replace("\f", "\\f");
        content = content.replace("\n", "\\n");
        content = content.replace("\r", "\\r");
        content = content.replace("\t", "\\t");
        return content;

    }

    public final static String html(String content) {
        return escapeHtml(content);
    }

    private final static String escapeHtml(String content) {
        if (StringUtils.isBlank(content)) {
            return "";
        }
        content = content.replace("<", "&#60;");
        content = content.replace(">", "&#62;");
        content = content.replace("\"", "&#34;");
        content = content.replace("'", "&#39;");
        content = content.replaceAll("&(?![\\w#]+;|#\\d+)", "&#38;");
        return content;
    }

    public static String delHTMLTag(String htmlStr){
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script=Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤script标签

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }
}